package com.totcy.atom.presenter;

import android.content.Context;

import com.totcy.atom.CommonValues;
import com.totcy.atom.bean.HeWeatherBase;
import com.totcy.atom.bean.response.HeWeatherResponse;
import com.totcy.atom.model.WeatherModel;
import com.totcy.atom.presenter.contract.WeatherContract;
import com.totcy.baselibrary.base.mvpbase.BasePresenterImpl;
import com.totcy.baselibrary.utils.SPUtil;

/**
 * description: 和风天气presenter，获取天气数据集合
 * autour: Totcy
 * date: 2018/1/20 14:33
 * update: 2018/1/20
 */
public class WeatherPresenter extends BasePresenterImpl<WeatherContract.View, WeatherContract.Model> implements WeatherContract.Presenter {

    public WeatherPresenter(Context context) {
        super(context);
    }

    @Override
    public WeatherContract.Model attachModel() {
        return new WeatherModel(mContext);
    }

    /**
     * Presenter 调用 model 获取数据 获取和风天气集合数据
     * 3-7天天气预报、实况天气、逐小时预报以及生活指数
     *
     * @param location location=北京
     *                 location=beijing
     *                 location=CN101010100
     *                 location= 60.194.130.1
     *                 location=120.343,36.088
     */
    @Override
    public void getWeatherInfoList(String location) {
        if (isModelAttached()) {
            getModel().getWeatherInfoList(location, new WeatherContract.Model.DataCallback() {
                @Override
                public void onWeatherInfo(HeWeatherResponse weatherResponse) {
                    if (isViewAttached()) {
                        HeWeatherBase weatherBase = null;
                        if (weatherResponse.getHeWeather6().size() > 0) {
                            weatherBase = weatherResponse.getHeWeather6().get(0);
                            if ("ok".equals(weatherBase.getStatus())) {
                                weatherBase.getNow().setAddress(weatherBase.getBasic().getAdmin_area() +
                                        " " + weatherBase.getBasic().getLocation());
                                //保存城市
                                SPUtil.put(mContext, CommonValues.KEY_WEATHER_ADDRESS, weatherBase.getBasic().getLocation());

                                getView().onNowWeather(weatherBase.getNow());
                                getView().on3To7Weather(weatherBase.getDaily_forecast());
                                getView().onHourlyWeather(weatherBase.getHourly());
                                getView().onLifeIndex(weatherBase.getLifestyle());
                                return;
                            }
                        }
                        getView().onDataFail(0, weatherBase != null ? weatherBase.getStatus() : "天气信息获取失败");

                    }
                }

                @Override
                public void onDataNotAvailable(int code, String message) {
                    if (isViewAttached()) {
                        getView().onDataFail(code, message);
                    }
                }
            });
        }
    }
}
